package com.wuge.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.Map;


import com.wuge.gulimall.product.entity.CategoryEntity;
import com.wuge.gulimall.product.entity.ProductAttrValueEntity;
import com.wuge.gulimall.product.service.CategoryService;
import com.wuge.gulimall.product.service.ProductAttrValueService;
import com.wuge.gulimall.product.vo.AttrDetailResVo;
import com.wuge.gulimall.product.vo.AttrVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.wuge.gulimall.product.entity.AttrEntity;
import com.wuge.gulimall.product.service.AttrService;
import com.wuge.common.utils.PageUtils;
import com.wuge.common.utils.R;

import javax.annotation.Resource;


/**
 * 商品属性
 *
 * @author ñÒ¹ÚÎé
 * @email 1712378864@qq.com
 * @date 2021-06-27 14:36:04
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;
    @Autowired
    private CategoryService categoryService;
    @Resource
    private ProductAttrValueService productAttrValueService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:attr:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }
///product/attr/base/list/{catelogId}
@RequestMapping("/{type}/list/{catelogId}")
//@RequiresPermissions("product:attr:list")
public R baselist(@RequestParam Map<String, Object> params, @PathVariable("catelogId")Long attrId,@PathVariable("type") String type){
    PageUtils page = attrService.queryPage(params,attrId,type);

    return R.ok().put("page", page);
}
    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
  //  @RequiresPermissions("product:attr:info")
    public R info(@PathVariable("attrId") Long attrId){
		AttrEntity attr = attrService.getById(attrId);
        List<Long> longs = categoryService.parentId(attr.getCatelogId());

        AttrDetailResVo attrDetailResVo=new AttrDetailResVo();
        BeanUtils.copyProperties(attr,attrDetailResVo);
        attrDetailResVo.setCatelogPath(longs);
        Long groupId = attrService.getGroupId(attrId);
        attrDetailResVo.setAttrGroupId(groupId);
        return R.ok().put("attr", attrDetailResVo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
   // @RequiresPermissions("product:attr:save")
    public R save(@RequestBody AttrVo attr){
		attrService.save(attr);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
   // @RequiresPermissions("product:attr:update")
    public R update(@RequestBody AttrVo attr){
		attrService.updateAttr(attr);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
   // @RequiresPermissions("product:attr:delete")
    public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }
///product/attr/base/listforspu/{spuId}
@RequestMapping("/base/listforspu/{spuId}")
//  @RequiresPermissions("product:attr:info")
public R infoBase(@PathVariable("spuId") Long spuId){
    List<ProductAttrValueEntity> list=productAttrValueService.getBaseList(spuId);
    return R.ok().put("data",list);
}
///product/attr/update/{spuId}
@RequestMapping("/update/{spuId}")
// @RequiresPermissions("product:attr:update")
public R updateBySpuId(@RequestBody List<ProductAttrValueEntity> list,@PathVariable("spuId") Long spuId){
    productAttrValueService.updateAttr(list,spuId);

    return R.ok();
}
}
